using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace shuju_jiange
{
    public partial class Form2 : Form
    {
        private List<mypoint> datalist;
        //private Form1 from1;
        public Form2()
        {
            InitializeComponent();
        }

        public Form2(List<mypoint> list,bool ischazhi)
        {
            InitializeComponent();
            datalist = list;
            this.checkBox2.Checked = ischazhi;
           
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            RefreshChart();
        }

        private void RefreshChart()
        {
            chart1.Titles.Clear();
            chart1.Series.Clear();
            chart1.Titles.Add("图形显示");

            chart1.Series.Add(new Series());
            chart1.Series.Add(new Series());
            chart1.Series.Add(new Series());
            chart1.Series.Add(new Series()); // X值
            chart1.Series.Add(new Series()); // Y值
            chart1.Series.Add(new Series()); // Z值
            chart1.Series.Add(new Series()); // 理论间隔-Z值

            chart1.Series[0].ChartType = SeriesChartType.Column;
            chart1.Series[1].ChartType = SeriesChartType.Column;
            chart1.Series[2].ChartType = SeriesChartType.Column;
            chart1.Series[3].ChartType = SeriesChartType.Column; // X值用柱状图
            chart1.Series[4].ChartType = SeriesChartType.Column; // Y值用柱状图
            chart1.Series[5].ChartType = SeriesChartType.Column; // Z值用柱状图
            chart1.Series[6].ChartType = SeriesChartType.Column; // 理论间隔-Z值用柱状图
            
            chart1.Series[1].Color = Color.Red;
            chart1.Series[3].Color = Color.Blue; // X值颜色
            chart1.Series[4].Color = Color.Green; // Y值颜色
            chart1.Series[5].Color = Color.Orange; // Z值颜色
            chart1.Series[6].Color = Color.Purple; // 理论间隔-Z值颜色

            chart1.Series[0].LegendText = "实际间隔";
            chart1.Series[1].LegendText = "理论间隔";
            chart1.Series[2].LegendText = "差值";
            chart1.Series[3].LegendText = "X值";
            chart1.Series[4].LegendText = "Y值";
            chart1.Series[5].LegendText = "Z值";
            chart1.Series[6].LegendText = "理论间隔-Z";

            // 根据复选框状态设置系列可见性
            chart1.Series[0].Enabled = chkShowActualInterval.Checked;
            chart1.Series[1].Enabled = chkShowTheoreticalInterval.Checked;
            chart1.Series[2].Enabled = chkShowDifference.Checked;
            chart1.Series[3].Enabled = chkShowX.Checked;
            chart1.Series[4].Enabled = chkShowY.Checked;
            chart1.Series[5].Enabled = chkShowZ.Checked;
            chart1.Series[6].Enabled = chkShowTheoreticalMinusZ.Checked;

            if (checkBox1.Checked)
            {
                chart1.Series[0].Label = "#VAL{0.00}";
                chart1.Series[0].ToolTip = "#VALX,\r#VAL{0.00}";
                chart1.Series[1].Label = "#VAL{0.00}";
                chart1.Series[1].ToolTip = "#VALX,\r#VAL{0.00}";
                chart1.Series[2].Label = "#VAL{0.00}";
                chart1.Series[2].ToolTip = "#VALX,\r#VAL{0.00}";
                chart1.Series[3].Label = "#VAL{0.00}";
                chart1.Series[3].ToolTip = "#VALX,\r#VAL{0.00}";
                chart1.Series[4].Label = "#VAL{0.00}";
                chart1.Series[4].ToolTip = "#VALX,\r#VAL{0.00}";
                chart1.Series[5].Label = "#VAL{0.00}";
                chart1.Series[5].ToolTip = "#VALX,\r#VAL{0.00}";
                chart1.Series[6].Label = "#VAL{0.00}";
                chart1.Series[6].ToolTip = "#VALX,\r#VAL{0.00}";
            }

            if (checkBox2.Checked)
            {
                foreach (mypoint m in datalist)
                {
                    m.chazhi = m.y- m.red;
                }
               
            }
            else
            {
                foreach (mypoint m in datalist)
                {
                    m.chazhi = m.red - m.y;
                }
               
                // from1.showData(from1.datalist);
            }

            /* 关键缩放代码 start  */
            // Zoom into the X axis
            //chart1.ChartAreas[0].AxisX.ScaleView.Zoom(2, 3);

            // Enable range selection and zooming end user interface
             chart1.ChartAreas[0].CursorX.IsUserEnabled = true;
            chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
            chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;

          //  chart1.ChartAreas[1].CursorX.IsUserEnabled = true;
          //  chart1.ChartAreas[1].CursorX.IsUserSelectionEnabled = true;
          // chart1.ChartAreas[1].AxisX.ScaleView.Zoomable = true;

            //将滚动内嵌到坐标轴中
            chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
           // chart1.ChartAreas[1].AxisX.ScrollBar.IsPositionedInside = true;

            // 设置滚动条的大小
            chart1.ChartAreas[0].AxisX.ScrollBar.Size = 20;
           // chart1.ChartAreas[1].AxisX.ScrollBar.Size = 20;

            // 设置滚动条的按钮的风格，下面代码是将所有滚动条上的按钮都显示出来
            chart1.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All;
           // chart1.ChartAreas[1].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All;

            // 设置自动放大与缩小的最小量
            chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollSize = double.NaN;
            chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSize = 2;
           // chart1.ChartAreas[1].AxisX.ScaleView.SmallScrollSize = double.NaN;
           // chart1.ChartAreas[1].AxisX.ScaleView.SmallScrollMinSize = 2;

            /* 关键缩放代码 end  */

            foreach (mypoint m in datalist)
            {
                chart1.Series[0].Points.Add(new DataPoint(m.x,m.y));
                chart1.Series[1].Points.Add(new DataPoint(m.x, m.red));
                chart1.Series[2].Points.Add(new DataPoint(m.x, m.chazhi));
                chart1.Series[3].Points.Add(new DataPoint(m.x, m.X));
                chart1.Series[4].Points.Add(new DataPoint(m.x, m.Y));
                chart1.Series[5].Points.Add(new DataPoint(m.x, m.Z));
                chart1.Series[6].Points.Add(new DataPoint(m.x, m.TheoreticalMinusZ));
            }
        }

        private void SeriesCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            RefreshChart();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            RefreshChart();
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            RefreshChart();
        }
    }
    public class mypoint
    {
        public mypoint(int i,String s,int redvalue)
        {
            x = i;
            int temp;
            int.TryParse(s, out temp);
            y = temp;
            red = redvalue;
            chazhi = redvalue - temp;
        }

        public mypoint(int i, String s, int redvalue, float xValue, float yValue, float zValue, float theoreticalMinusZ)
        {
            x = i;
            int temp;
            int.TryParse(s, out temp);
            y = temp;
            red = redvalue;
            chazhi = redvalue - temp;
            X = xValue;
            Y = yValue;
            Z = zValue;
            TheoreticalMinusZ = theoreticalMinusZ;
        }

        public int x { get; set; }
        public int y { get; set; }
        public int red { get; set; }
        public int chazhi { get; set; }
        public String lab { get; set; }
        public String msg { get; set; }
        
        // 新增的4个属性
        public float X { get; set; }
        public float Y { get; set; }
        public float Z { get; set; }
        public float TheoreticalMinusZ { get; set; }
    }
}
